#version 330

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform vec4 lightcolorambient;
uniform vec4 objectcolorambient;


uniform vec3 lightPosition;
uniform vec4 objectcolordiffuse;
uniform vec4 lightcolordiffuse;

uniform vec4 objectcolorspec;
uniform vec4 lightcolorspec;


out vec4 outcolor;


void main()
{
	gl_Position = projection * view * model * vec4(position, 1.0);
	
	//Ambient
	vec4 ac = lightcolorambient * objectcolorambient;

	//Diffuse
	vec3 norm = mat3(transpose(inverse(view * model))) * normal;
	norm = normalize(norm);
	vec4 vertexPos = view * model * vec4(position, 1.0);
	vec4 pos = view * model * vec4(lightPosition, 1.0);
	vec3 lightDir = normalize(pos.xyz - vertexPos.xyz);
	float diff = max(dot(norm, lightDir), 0.0);
	vec4 dc = diff * lightcolordiffuse * objectcolordiffuse;

	//Specular
	vec4 viewDir = -pos;
	vec3 viewDirVec3 = normalize(viewDir.xyz);
	vec3 reflectDir = reflect(-lightDir, norm);
	vec3 refelctDir = normalize(reflectDir);
	float spec = pow(max(dot(viewDirVec3, reflectDir), 0.0), 32);
	vec4 sc = spec * lightcolorspec * lightcolorspec;
	
	outcolor = ac + dc + sc;
}
